package com.zoshine.zzg.service;

import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.GoodsHistoryDetail;
import com.zoshine.zzg.pojo.GoodsPublishDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 田沛凌
 * @time 2017/8/11 10:55
 */
@Service
@Transactional
public class GoodsHistoryDetailService extends BaseService<GoodsHistoryDetail>{

    private GoodsPublishDetailService goodsPublishDetailService;
    @Autowired
    public void setGoodsPublishDetailService(GoodsPublishDetailService goodsPublishDetailService) {
        this.goodsPublishDetailService = goodsPublishDetailService;
    }

    /**
     * 批量插入历史商品表
     * @param idStr 上架商品ID集合
     */
    public void addGoodsHistoryDetail(String idStr){
        //为空判断
        if (idStr == null || idStr.length()==0) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }
        String[] pidArr = idStr.split(",");
        List<GoodsHistoryDetail> historyDetails=new ArrayList<>();
        for (String id : pidArr) {
            Long settlementId=Long.parseLong(id);
            GoodsPublishDetail goodsPublishDetail = goodsPublishDetailService.get(settlementId);
            if (goodsPublishDetail == null){
                throw new AppException(ErrorMessage.MODEL_CAN_NOT_FOUND);
            }
            //插入到历史表中
            GoodsHistoryDetail goodsHistoryDetail=new GoodsHistoryDetail();
            goodsHistoryDetail.setId(super.baseIdWorker.createId());
            goodsHistoryDetail.setGoodsId(goodsPublishDetail.getId());
            goodsHistoryDetail.setOrgId(goodsPublishDetail.getOrgId());
            goodsHistoryDetail.setGoodsName(goodsPublishDetail.getGoodsName());
            goodsHistoryDetail.setGoodsType(goodsPublishDetail.getGoodsType());
            goodsHistoryDetail.setGoodsBarCode(goodsPublishDetail.getGoodsBarCode());
            goodsHistoryDetail.setGoodsMarketPrice(goodsPublishDetail.getGoodsMarketPrice());
            goodsHistoryDetail.setGoodsSellPrice(goodsPublishDetail.getGoodsSellPrice());
            goodsHistoryDetail.setGoodsSettlementPrice(goodsPublishDetail.getGoodsSettlementPrice());
            goodsHistoryDetail.setGoodsSpec(goodsPublishDetail.getGoodsSpec());
            goodsHistoryDetail.setGoodsUnit(goodsPublishDetail.getGoodsUnit());
            goodsHistoryDetail.setGoodsImg(goodsPublishDetail.getGoodsImg());
            goodsHistoryDetail.setGoodsStatus(goodsPublishDetail.getGoodsStatus());
            goodsHistoryDetail.setBoxSpec(goodsPublishDetail.getBoxSpec());
            goodsHistoryDetail.setCreateTime(new Date());
            goodsHistoryDetail.setUpdateTime(new Date());
            historyDetails.add(goodsHistoryDetail);
        }
    super.add(historyDetails);
    }
}
